/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWindow.java
 *
 * Created on May 18, 2011, 6:02:48 PM
 */
package com.mycompany.inserthyperlinks;

import com.mycompany.inserthyperlinks.utils.DirectoryRestrictedFileSystemView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.poi.ss.usermodel.Workbook;

/**
 *
 * @author asevciuc
 */
public class MainWindow extends javax.swing.JFrame {

    File file = null;
    List<File> imageFilesList = new ArrayList<File>();

    /** Creates new form MainWindow */
    public MainWindow() {
        initComponents();
        FileFilter fileFilter = new FileNameExtensionFilter("Excel files", "xlsx", "xls");
        chooseFileName.setFileFilter(fileFilter);
        chooseFileName.setCurrentDirectory(new File(System.getProperty("user.dir")));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chooseFileName = new javax.swing.JFileChooser();
        imagesChooser = new javax.swing.JFileChooser();
        fillWorkBook = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        fileName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        filesList = new java.awt.List();
        imageChooserOpen = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        fromRowNumber = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        columnNumber = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        columnNumberDate = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        columnNumberLei = new javax.swing.JTextField();
        columnNumberCode = new javax.swing.JTextField();
        sheetNames = new javax.swing.JComboBox();

        chooseFileName.setCurrentDirectory(new java.io.File("\\\\"));
            chooseFileName.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
            chooseFileName.setFileFilter(null);
            chooseFileName.setFileHidingEnabled(false);
            chooseFileName.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    chooseFileNameActionPerformed(evt);
                }
            });

            imagesChooser.setMultiSelectionEnabled(true);
            imagesChooser.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    imagesChooserActionPerformed(evt);
                }
            });

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Insert hiperlinks to file");
            setResizable(false);

            fillWorkBook.setText("Fill data");
            fillWorkBook.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    fillWorkBookMouseClicked(evt);
                }
            });
            fillWorkBook.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    fillWorkBookActionPerformed(evt);
                }
            });

            jLabel4.setText("File name");

            fileName.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    fileNameMouseClicked(evt);
                }
            });

            jLabel5.setText("Sheet name");

            filesList.setMultipleMode(true);
            filesList.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    filesListKeyPressed(evt);
                }
            });

            imageChooserOpen.setText("Add files");
            imageChooserOpen.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    imageChooserOpenMouseClicked(evt);
                }
            });

            jLabel1.setText("From row number");

            fromRowNumber.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    fromRowNumberActionPerformed(evt);
                }
            });

            jLabel3.setText("Column number: Link");

            jLabel6.setText("Column number: Date");

            jLabel7.setText("Column number: Lei");

            jLabel8.setText("Column number: Code");

            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(jLabel8))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(jLabel3))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(jLabel7))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(jLabel1)))
                            .addGap(34, 34, 34)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(fromRowNumber, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                                .addComponent(columnNumberCode)
                                .addComponent(columnNumberLei)
                                .addComponent(columnNumber)
                                .addComponent(columnNumberDate)))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel6)))
                    .addContainerGap(25, Short.MAX_VALUE))
            );

            jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {columnNumber, fromRowNumber});

            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(fromRowNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(columnNumberDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(11, 11, 11)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(columnNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(columnNumberLei, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(16, 16, 16)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(columnNumberCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap())
            );

            jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {columnNumber, fromRowNumber});

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel5))
                            .addGap(10, 10, 10)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(sheetNames, 0, 202, Short.MAX_VALUE)
                                .addComponent(fileName, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(54, 54, 54))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(19, 19, 19)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                        .addComponent(filesList, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(imageChooserOpen))
                    .addContainerGap(26, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(filesList, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(16, 16, 16)
                    .addComponent(imageChooserOpen))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(3, 3, 3)
                            .addComponent(jLabel4))
                        .addComponent(fileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(14, 14, 14)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(sheetNames, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            );

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(10, 10, 10)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(290, Short.MAX_VALUE)
                    .addComponent(fillWorkBook)
                    .addGap(221, 221, 221))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(11, 11, 11)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(fillWorkBook)
                    .addGap(17, 17, 17))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents

    private void fromRowNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fromRowNumberActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fromRowNumberActionPerformed

    private void fillWorkBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fillWorkBookActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fillWorkBookActionPerformed

    private void fillWorkBookMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fillWorkBookMouseClicked
        OfficeWorkBook book = new OfficeWorkBook();
        try {
            book.testUpdateFile(file,fileName.getText(),sheetNames.getSelectedItem().toString(),
                    Integer.parseInt(columnNumber.getText()),
                    Integer.parseInt(columnNumberDate.getText()),Integer.parseInt(columnNumberCode.getText()),
                    Integer.parseInt(fromRowNumber.getText()),imageFilesList);
            JOptionPane.showMessageDialog(this,"New upadated sheet was created");
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage());
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage());
        }

    }//GEN-LAST:event_fillWorkBookMouseClicked

    private void fileNameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fileNameMouseClicked
        chooseFileName.showOpenDialog(fileName);
    }//GEN-LAST:event_fileNameMouseClicked

    private void chooseFileNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseFileNameActionPerformed
        file = chooseFileName.getSelectedFile();
        fileName.setText(file != null ? file.getName() : "");
        sheetNames.removeAllItems();
        try {
            Workbook workbook=OfficeWorkBook.getWorkBook(file);
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                sheetNames.addItem(workbook.getSheetAt(i).getSheetName());
            }
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage());
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage());
        }

    }//GEN-LAST:event_chooseFileNameActionPerformed

    private void imagesChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imagesChooserActionPerformed
        File[] imageFiles = imagesChooser.getSelectedFiles();
        for (File imageFile : imageFiles) {
            filesList.add(deleteFirstPartOfFilePath(file.getParent(),imageFile.getAbsolutePath()));
            imageFilesList.add(imageFile);
        }
        
    }//GEN-LAST:event_imagesChooserActionPerformed
    private String deleteFirstPartOfFilePath(String parent,String fullPath){
        parent= parent.substring(parent.lastIndexOf("\\")+1);
        String path=fullPath.substring(fullPath.indexOf(parent)+parent.length()+1);
        return path.replace("\\", "/");
    }
    private void imageChooserOpenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imageChooserOpenMouseClicked
        if(file!=null){       
            imagesChooser.setCurrentDirectory(file);
            DirectoryRestrictedFileSystemView drfsv = new DirectoryRestrictedFileSystemView(file);
            imagesChooser.setFileSystemView(drfsv); 
            FileFilter imageFilter = new FileNameExtensionFilter("JPG & PNG files", "jpg", "png");
            imagesChooser.setFileFilter(imageFilter);
            imagesChooser.showOpenDialog(fileName);
        }else{
            JOptionPane.showMessageDialog(this, "Choose file first");
        }

    }//GEN-LAST:event_imageChooserOpenMouseClicked

    private void filesListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_filesListKeyPressed
        if (evt.getKeyCode() == 127) {
            int index = filesList.getSelectedIndex();
            filesList.remove(index);
            imageFilesList.remove(index);
        }
    }//GEN-LAST:event_filesListKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JFileChooser chooseFileName;
    javax.swing.JTextField columnNumber;
    javax.swing.JTextField columnNumberCode;
    javax.swing.JTextField columnNumberDate;
    javax.swing.JTextField columnNumberLei;
    javax.swing.JTextField fileName;
    java.awt.List filesList;
    javax.swing.JButton fillWorkBook;
    javax.swing.JTextField fromRowNumber;
    javax.swing.JButton imageChooserOpen;
    javax.swing.JFileChooser imagesChooser;
    javax.swing.JLabel jLabel1;
    javax.swing.JLabel jLabel3;
    javax.swing.JLabel jLabel4;
    javax.swing.JLabel jLabel5;
    javax.swing.JLabel jLabel6;
    javax.swing.JLabel jLabel7;
    javax.swing.JLabel jLabel8;
    javax.swing.JPanel jPanel1;
    javax.swing.JPanel jPanel2;
    javax.swing.JComboBox sheetNames;
    // End of variables declaration//GEN-END:variables
}
